
// rtmp://gwm-000-cn-0448.bcloud365.net:9016/talk_2e24b0d33c27886b/Mnx8Y2U3ZTg2YmYxOGZmZTE3YmQ0NDRkZWUxMTcyN2FjZTd8fDJlMjRiMGQzM2MyNzg4NmJ8fDYwOTJlNTg5OWNjODIzOTYzYjg5OWZjNDg12NTdiNmNiOThiOWQ1NDkyYzY5MDA0OTlmNDc4ZjVkMzgzMDNiODJ8fHJ0bXB8fDE3MTQwMzYzNDk0Nzh8fDE3MTQwNzIzNDkzMjd8fEdXTQ%3D%3D.376a8ab00c48614ed6326b3d2bc211d9
export const twoWayVideoMixins = {
  data() {
    return {
      appDeviceList:[],
      page:1,
      device:null,
      supportSwitchType:'',
      videoWidth:320,
      videoHeight:576,
      fps:12,
      videoPlayer:'h264',
      audioCodePlayer:'g711a',
      audioKHZPlayer:'8000',
      audioAccuracyPlayer:'16',
    }
  },
  onShow(){
  },
  onLoad(opt) {
  },
  methods: {
    wakeup(callback) {
      let data={
        sn:this.device.deviceNo,
        hostedControl:"1",//是否唤醒主控（空值或0：不唤醒主控；1：唤醒主控）
      }
      getApp().globalData.api.wakeup(data,this.device.token, (result) => {
        this.$logs({apiUrl: '低功耗唤醒设备接口',data:result})
        callback();
      });
    },
    // 多目门锁视频编码画面切换能力
    getdoorLocktability(Name = 'DecoderPram',callback) {
      let data = {
        Name: Name
      };
      let token = this.device.token;
      getApp().globalData.api.getabilityV3(data, token, (result) => {
        console.log('获取门锁能力集',result);
        if(result.code == 2000){
          let res = result.data
          if(res.Ret == 100){
            if(Name == 'DecoderPram'){
              let decoderPram = res.DecoderPram
              this.videoWidth = decoderPram.Video[0].Res[0].W
              this.videoHeight = decoderPram.Video[0].Res[0].H
              this.fps = decoderPram.Video[0].Res[0].FPS  || '12'
              this.videoPlayer = decoderPram.Video[0].Enc || 'h264'
              this.audioCodePlayer = decoderPram.Audio[0].Enc || 'g711a'
              this.audioKHZPlayer = decoderPram.Audio[0].SR[0] || '8000'
              this.audioAccuracyPlayer = (decoderPram.Audio[0].SB&&decoderPram.Audio[0].SB[0]) || '16'
              // this.videoWidth = 320//532
              // this.videoHeight = 576//320
              // this.fps = 12
              console.log('宽高帧率',this.videoWidth,this.videoHeight,this.fps);
              callback()
            }

          }else if(res.Ret == 527){
            //消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
            this.wakeup(()=>{
              this.getdoorLocktability(Name)
            })
          }else if(res.Ret !=103 && res.Ret !=102){
            uni.showToast({
              title:getApp().globalData.errCodeMsg.RetCodeMsg[res.Ret] || res.RetMsg,
              icon:'none'
            })
          }
        }else{
          uni.showToast({
            title:getApp().globalData.errCodeMsg.CodeMsg[result.code] || result.msg,
            icon:'none'
          })
        }
      },false);
    },
    getLoginSetting(callback) {
      uni.showLoading({
        mask: true,
      });
      let token = this.device.token;
      console.log('device',this.device)
      let data = {
        encryptType: 'DISABLE',
        LoginType: 'DVRIP-Web',
        UserName: 'admin',
        PassWord: this.device.devPassWord,
        Name: 'generalinfo'
      };
      if (this.device.adminToken) {
        data.LoginToken = this.device.adminToken
        data.encryptType = 'TOKEN'
      }
      getApp().globalData.api.deviceLoginV3(data, token, (result) => {
        uni.hideLoading();
        console.log('result---deviceLogin-->',result)
        if (result.code === 2000) {
          // 登录成功 或 已登录
          if (result.data.Ret === 100 || result.data.Ret === 104) {
            console.log('成功')
            callback(result)
          } // 出错
          else {
            uni.showToast({
              icon: 'none',
              title: getApp().globalData.errCodeMsg.RetCodeMsg[result.data
                  .Ret] || result.data.RetMsg,
            });
          }
        } else {
          uni.showToast({
            icon: 'none',
            title: getApp().globalData.errCodeMsg.CodeMsg[result.code] || result
                .msg
          });
        }
      },false)
    },
    // 获取设备最新的adminToken
    getAdminTokenAndDeviceToken(callback){
        if(this.sn){
          getApp().globalData.api.getDeviceTokenV3({sns:[this.sn]}, (result) => {
            this.device = {
              deviceNo:this.sn,
              token:result[0].token
            }
            let data = {
              sn:this.sn,
            }
            getApp().globalData.api.getDeviceLoginToken(data,this.device.token,(result1)=>{
              console.log('获取设备登录令牌',result1)
              this.device.adminToken = result1.loginToken
              this.getLoginSetting(()=>{
                this.getdoorLocktability('DecoderPram',()=>{
                  this.getTwoWayVideoUrl(callback)
                })
              })
            })
          })
        }else{
          uni.showToast({
            icon:'none',
            title:'设备序列号不能为空'
          })
        }
    },
    //   获取具体设备视频双向对讲地址
    getTwoWayVideoUrl(callback){
      console.log('获取双向视频对讲地址')
      let token = this.device.token;
      let width = '360'
      let height = '640'
      let systemInfo = uni.getSystemInfo()
      if(systemInfo.deviceModel == 'MT2110'){  // onePlus9RT
        width = '270'
        height = '480'
      }
      let data = {
        mediaType: 'webrtc',
        channel: '0',
        audioCode: 'acc',
        username: this.device.devUserName,
        password: this.device.devPassWord,
        // userToken: uni.getStorageSync('accessToken'),
        appKey: getApp().globalData.api.appKey,
        encryptType: 'DISABLE',
        audioCodePlayer:this.audioCodePlayer,
        audioAccuracyPlayer:String(this.audioAccuracyPlayer),
        videoPlayer:this.videoPlayer,
        widthPlayer:String(this.videoWidth),
        heightPlayer:String(this.videoHeight),
        fpsPlayer:String(this.fps),
        audioKHZPlayer:String(this.audioKHZPlayer)
      };
      if (this.device.adminToken) {
        data.authentication = this.device.adminToken
        data.encryptType = 'TOKEN'
      }
      console.log('data--->',data)
      console.log('token--->',token)
      getApp().globalData.api.getTwoWayVideoUrlV3(data, token, (result) => {
        console.log('result--->',result)
        if(result.code === 4120){
          uni.showToast({
            title:'此设备处于深度休眠状态，请唤醒后重试',
            icon:'none'
          })
          return
        }
        if(result.code == 2000){
          let Ret = result.data.Ret
          if (Ret === 100 || Ret === 104) {
            this.$logs({apiUrl: '获取双向视频对讲地址',sn:this.device.deviceNo,data:data,result:result})
            this.talkUrlInput = result.data.url//+'?widthPlayer=360&heightPlayer=640'
            callback()
          }else if(Ret === 108){
            // 超时
            uni.showToast({
              title:'超时，请重试',
              icon:'none'
            })
            return
          }else {
            uni.showToast({
              title:'请重试',
              icon:'none'
            })
            return
          }
        }
      },false,false)
    },
  }
}